/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package empentity;

import java.math.BigDecimal;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.CreateException;
import javax.ejb.DuplicateKeyException;
import javax.sql.DataSource;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.NoSuchEntityException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.RemoveException;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author user
 */
public class EmpBean implements EntityBean {

    private String namedatasource = "OracleDS";
    private DataSource datasource;
    private EntityContext entitycontext;
    private BigDecimal empno;
    private String ename;
    private String job;
    private BigDecimal mgr;
    private java.sql.Date hiredate;
    private BigDecimal sal;
    private BigDecimal comm;
    private BigDecimal deptno;
    private boolean isChanged;

    public Collection ejbFindByDeptno(BigDecimal deptno) throws FinderException, RemoteException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = datasource.getConnection();
            preparedStatement = connection.prepareStatement("select empno "
                    + "from emp "
                    + "where deptno = ?");
            preparedStatement.setBigDecimal(1, deptno);
            ResultSet executeQuery = preparedStatement.executeQuery();
            ArrayList arrayList = new ArrayList();
            System.out.println("do not enter to while");
            while (executeQuery.next()) {
                System.out.println("enter to while");
                arrayList.add(executeQuery.getBigDecimal(1));
            }
            return arrayList;
        } catch (SQLException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } catch (NoSuchEntityException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } finally {
            closeConnection(connection, preparedStatement);
        }
    }

    public Collection ejbFindByJob(String job) throws FinderException, RemoteException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = datasource.getConnection();
            preparedStatement = connection.prepareStatement("select empno "
                    + "from emp "
                    + "where lower(job) LIKE lower('%" + job + "%')");
            ResultSet executeQuery = preparedStatement.executeQuery();
            ArrayList arrayList = new ArrayList();
            System.out.println("do not enter to while");
            while (executeQuery.next()) {
                System.out.println("enter to while");
                arrayList.add(executeQuery.getBigDecimal(1));
            }
            return arrayList;
        } catch (SQLException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } catch (NoSuchEntityException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } finally {
            closeConnection(connection, preparedStatement);
        }
    }

    public Collection ejbFindAll() throws FinderException, RemoteException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = datasource.getConnection();
            preparedStatement = connection.prepareStatement("select empno "
                    + "from emp");
            ResultSet executeQuery = preparedStatement.executeQuery();
            ArrayList arrayList = new ArrayList();
            while (executeQuery.next()) {
                arrayList.add(executeQuery.getBigDecimal(1));
            }
            return arrayList;
        } catch (SQLException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } catch (NoSuchEntityException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } finally {
            closeConnection(connection, preparedStatement);
        }
    }

    public BigDecimal ejbFindByEmpno(BigDecimal empno) throws FinderException, RemoteException {
        return ejbFindByPrimaryKey(empno);
    }

    public void setEntityContext(EntityContext ctx) throws EJBException, RemoteException {
        this.entitycontext = ctx;
        try {
            InitialContext initialContext = new InitialContext();
            datasource = (DataSource) initialContext.lookup(namedatasource);
        } catch (NamingException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        }
    }

    public void unsetEntityContext() throws EJBException, RemoteException {
        datasource = null;
        entitycontext = null;
    }

    public void ejbRemove() throws RemoveException, EJBException, RemoteException {
        empno = (BigDecimal) entitycontext.getPrimaryKey();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = datasource.getConnection();
            preparedStatement = connection.prepareStatement("delete from emp "
                    + "where empno = ?");
            preparedStatement.setBigDecimal(1, empno);
            if (preparedStatement.executeUpdate() < 1) {
                throw new NoSuchEntityException("Could not remove entity by: " + preparedStatement.toString());
            }
        } catch (SQLException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } catch (NoSuchEntityException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } finally {
            closeConnection(connection, preparedStatement);
        }
    }

    public void ejbActivate() throws EJBException, RemoteException {
    }

    public void ejbPassivate() throws EJBException, RemoteException {
    }

    public void ejbLoad() throws EJBException, RemoteException {
        empno = (BigDecimal) entitycontext.getPrimaryKey();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = datasource.getConnection();
            preparedStatement = connection.prepareStatement("select ename, job, mgr, hiredate, sal, comm, deptno "
                    + "from emp "
                    + "where empno = ?");
            preparedStatement.setBigDecimal(1, empno);
            ResultSet executeQuery = preparedStatement.executeQuery();
            if (!executeQuery.next()) {
                throw new NoSuchEntityException("Not found entity by: " + preparedStatement.toString());
            }
            ename = executeQuery.getString(1);
            job = executeQuery.getString(2);
            mgr = executeQuery.getBigDecimal(3);
            hiredate = executeQuery.getDate(4);
            sal = executeQuery.getBigDecimal(5);
            comm = executeQuery.getBigDecimal(6);
            deptno = executeQuery.getBigDecimal(7);
        } catch (SQLException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } catch (NoSuchEntityException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } finally {
            closeConnection(connection, preparedStatement);
        }
    }

    public void ejbStore() throws EJBException, RemoteException {
        if (!isChanged) {
            return;
        }
        empno = (BigDecimal) entitycontext.getPrimaryKey();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = datasource.getConnection();
            preparedStatement = connection.prepareStatement("update emp "
                    + "set ename = ?, job = ?, mgr = ?, hiredate = ?, sal = ?, comm = ?, deptno = ? "
                    + "where empno = ?");
            preparedStatement.setString(1, ename);
            preparedStatement.setString(2, job);
            preparedStatement.setBigDecimal(3, mgr);
            preparedStatement.setDate(4, hiredate);
            preparedStatement.setBigDecimal(5, sal);
            preparedStatement.setBigDecimal(6, comm);
            preparedStatement.setBigDecimal(7, deptno);
            preparedStatement.setBigDecimal(8, empno);
            if (preparedStatement.executeUpdate() < 1) {
                throw new NoSuchEntityException("Could not update entity by: " + preparedStatement.toString());
            }
        } catch (SQLException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } catch (NoSuchEntityException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } finally {
            closeConnection(connection, preparedStatement);
        }
        isChanged = false;
    }

    public void closeConnection(Connection con, Statement state) {
        try {
            if (state != null) {
                state.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public BigDecimal ejbCreateEmp(BigDecimal empno, String ename, String job, java.sql.Date hiredate, BigDecimal salary, BigDecimal deptno) throws CreateException, RemoteException {
        try {
            ejbFindByPrimaryKey(empno);
            throw new DuplicateKeyException("The entity with " + empno + " primary key exists!");
        } catch (ObjectNotFoundException ex) {
            this.empno = empno;
            this.ename = ename;
            this.job = job;
            this.hiredate = hiredate;
            this.sal = salary;
            this.deptno = deptno;
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            try {
                connection = datasource.getConnection();
                preparedStatement = connection.prepareStatement("insert into emp(empno, ename, job, hiredate, sal, deptno) "
                        + "values(?, ?, ?, ?, ?, ?)");
                preparedStatement.setBigDecimal(1, empno);
                preparedStatement.setString(2, ename);
                preparedStatement.setString(3, job);
                preparedStatement.setDate(4, hiredate);
                preparedStatement.setBigDecimal(5, salary);
                preparedStatement.setBigDecimal(6, deptno);
                if (preparedStatement.executeUpdate() < 1) {
                    throw new CreateException("Could not insert: " + preparedStatement.toString());
                }
                return this.empno;
            } catch (SQLException ex1) {
                Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex1);
                throw new EJBException(ex);
            } finally {
                closeConnection(connection, preparedStatement);
            }
        } catch (FinderException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        }
        //return null;
    }

    public void ejbPostCreateEmp(BigDecimal empno, String ename, String job, java.sql.Date hiredate, BigDecimal salary, BigDecimal deptno) throws CreateException, RemoteException {
    }

    public BigDecimal ejbFindByPrimaryKey(BigDecimal empno) throws ObjectNotFoundException, FinderException, RemoteException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = datasource.getConnection();
            preparedStatement = connection.prepareStatement("select empno "
                    + "from emp "
                    + "where empno = ?");
            preparedStatement.setBigDecimal(1, empno);
            ResultSet executeQuery = preparedStatement.executeQuery();
            if (!executeQuery.next()) {
                throw new ObjectNotFoundException("Not found entity by primary key:" + empno);
            }
            return executeQuery.getBigDecimal(1);
        } catch (SQLException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } catch (NoSuchEntityException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } finally {
            closeConnection(connection, preparedStatement);
        }
    }

    public Collection ejbFindByName(String ename) throws FinderException, RemoteException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = datasource.getConnection();
            preparedStatement = connection.prepareStatement("select empno "
                    + "from emp "
                    + "where lower(ename) LIKE lower('%" + ename + "%')");
            ResultSet executeQuery = preparedStatement.executeQuery();
            Collection list = new ArrayList();
            while (executeQuery.next()) {
                list.add(executeQuery.getBigDecimal(1));
            }
            return list;
        } catch (SQLException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } catch (NoSuchEntityException ex) {
            Logger.getLogger(EmpBean.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex);
        } finally {
            closeConnection(connection, preparedStatement);
        }
    }

    public BigDecimal getComm() throws RemoteException {
        return comm;
    }

    public BigDecimal getDeptno() throws RemoteException {
        return deptno;
    }

    public BigDecimal getEmpno() throws RemoteException {
        return empno;
    }

    public String getEname() throws RemoteException {
        return ename;
    }

    public java.sql.Date getHiredate() throws RemoteException {
        return hiredate;
    }

    public String getJob() throws RemoteException {
        return job;
    }

    public BigDecimal getMgr() throws RemoteException {
        return mgr;
    }

    public BigDecimal getSal() throws RemoteException {
        return sal;
    }

    public void setComm(BigDecimal comm) throws RemoteException {
        if (this.comm == comm) {
            return;
        }
        this.isChanged = true;
        this.comm = comm;
    }

    public void setDeptno(BigDecimal deptno) throws RemoteException {
        if (this.deptno == deptno) {
            return;
        }
        this.isChanged = true;
        this.deptno = deptno;
    }

    public void setEname(String ename) throws RemoteException {
        if (this.ename.equals(ename)) {
            return;
        }
        this.isChanged = true;
        this.ename = ename;
    }

    public void setHiredate(java.sql.Date hiredate) throws RemoteException {
        if (this.hiredate == hiredate) {
            return;
        }
        this.isChanged = true;
        this.hiredate = hiredate;
    }

    public void setJob(String job) throws RemoteException {
        if (this.job.equals(job)) {
            return;
        }
        this.isChanged = true;
        this.job = job;
    }

    public void setMgr(BigDecimal mgr) throws RemoteException {
        if (this.mgr == mgr) {
            return;
        }
        this.isChanged = true;
        this.mgr = mgr;
    }

    public void setSal(BigDecimal sal) throws RemoteException {
        if (this.sal == sal) {
            return;
        }
        this.isChanged = true;
        this.sal = sal;
    }
}
